// pages/hotel/searchResult/searchResult.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        provinceId: null,
        cityId: null,
        areaId: null,
        active: null, //酒店，民宿
        loading: false, //底部加载显示
        region: [], //地理位置，省市区
        date: '', //日期
        dateStart: '', //开始日期
        dateEnd: '', //结束日期
        show: false, //日期组件是否展示
        valueSearch: '', //搜索字段
        hotelList: [],
        EvaList: [],
        isLoading: false,
        hotelName: '',
        hotelType: null,
        priceList: [{
                text: '不限',
                value: 0
            },
            {
                text: '100-300',
                value: 1
            },
            {
                text: '300-500',
                value: 2
            },
            {
                text: '500-1000',
                value: 3
            },
            {
                text: '1000-2000',
                value: 4
            }
        ],
        sortList: [{
                text: '默认排序',
                value: 0
            },
            {
                text: '价格最低',
                value: 1
            },
            {
                text: '时间最新',
                value: 2
            },
        ],
        typeList: [{
                text: '酒店',
                value: 0
            },
            {
                text: '民宿',
                value: 1
            },
        ],
    },

    // 五角星评分变化事件
    onChangeStar(event) {
        this.setData({
            value: event.detail,
        });
    },

    // 区域下拉
    onClickNav({
        detail = {}
    }) {
        this.setData({
            mainActiveIndex: detail.index || 0,
        });
    },

    onClickItem({
        detail = {}
    }) {
        const activeId = this.data.activeId === detail.id ? null : detail.id;
        this.setData({
            activeId
        });
    },
    //   区域选择器
    onConfirmArea0() {
        this.selectComponent(1).toggle();
    },

    onConfirmArea1() {
        this.selectComponent(1).toggle();
    },

    onSwitchChange({
        detail
    }) {
        this.setData({
            switch1: detail
        });
    },

    //查询省市区id
    getArea() {
        console.log('11', this.data.region)
        wx.request({
            url: 'http://localhost:8080/system/province/list',
            method: 'GET',
            header: {
                Authorization: getApp().globalData.authorization
            },
            data: {
                province: this.data.region[0]
            },
            success: (resp) => {
                this.setData({
                    provinceId: resp.data.rows[0].provinceid,
                })
            },
        })
        wx.request({
            url: 'http://localhost:8080/system/city/list',
            method: 'GET',
            header: {
                Authorization: getApp().globalData.authorization
            },
            data: {
                city: this.data.region[1]
            },
            success: (resp) => {
                this.setData({
                    cityId: resp.data.rows[0].cityid,
                })
            },
        })
        wx.request({
            url: 'http://localhost:8080/system/area/list',
            method: 'GET',
            header: {
                Authorization: getApp().globalData.authorization
            },
            data: {
                area: this.data.region[2]
            },
            success: (resp) => {
                this.setData({
                    areaId: resp.data.rows[0].areaid,
                })
            },
        })
    },

    /**
     * 日期改变触发事件
     */
    dateClick() {
        this.setData({
            show: true
        });
    },
    onClose() {
        this.setData({
            show: false
        });
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getMonth() + 1}-${date.getDate()}`;
    },
    onConfirm(event) {
        const [start, end] = event.detail;
        this.setData({
            show: false,
            date: `${this.formatDate(start)}-${this.formatDate(end)}`,
            dateStart: `${this.formatDate(start)}`,
            dateEnd: `${this.formatDate(end)}`,
        });
    },

    // 搜索
    onSearch(event) {
        if (this.data.provinceId == null) {
            wx.request({
                url: 'http://localhost:8080/hotel/hotel/list',
                method: 'GET',
                header: {
                    Authorization: getApp().globalData.authorization
                },
                data: {
                    hotelName: event.detail,
                    hotelType: this.data.active
                },
                success: (resp) => {
                    console.log("haha", resp)
                    this.setData({
                        hotelList: resp.data.rows,
                        //totalPageCount: resp.data.data.pageCount,
                    })
                },
            })
        } else {
            wx.request({
                url: 'http://localhost:8080/hotel/hotel/list',
                method: 'GET',
                header: {
                    Authorization: getApp().globalData.authorization
                },
                data: {
                    hotelName: event.detail,
                    hotelType: this.data.active,
                    provinceId: this.data.provinceId,
                    cityId: this.data.cityId,
                    areaId: this.data.areaId
                },
                success: (resp) => {
                    console.log("haha", resp)
                    this.setData({
                        hotelList: resp.data.rows,
                        //totalPageCount: resp.data.data.pageCount,
                    })
                },
            })
        }
    },

    /**
     * 位置改变触发事件
     */
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
        this.getArea()
        this.getData()
    },

    getData() {
        console.log('123456', this.data.region)
        console.log('123456', this.data.provinceId)
        if (this.data.provinceId == null) {
            wx.request({
                url: 'http://localhost:8080/hotel/hotel/list',
                method: 'GET',
                header: {
                    Authorization: getApp().globalData.authorization
                },
                data: {
                    hotelType: this.data.active
                },
                success: (resp) => {
                    console.log('222', resp)
                    this.setData({
                        hotelList: resp.data.rows,
                        //totalPageCount: resp.data.data.pageCount,
                    })
                },
            })
        } else {
            wx.request({
                url: 'http://localhost:8080/hotel/hotel/list',
                method: 'GET',
                header: {
                    Authorization: getApp().globalData.authorization
                },
                data: {
                    hotelType: this.data.active,
                    provinceId: this.data.provinceId,
                    cityId: this.data.cityId,
                    areaId: this.data.areaId
                },
                success: (resp) => {
                    console.log('222', resp)
                    this.setData({
                        hotelList: resp.data.rows,
                        //totalPageCount: resp.data.data.pageCount,
                    })
                },
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        //接受上一个页面传过来的参数，同步页面数据
        var hotelData = JSON.parse(options.hotelData);
        this.setData({
            dateStart: hotelData.dateStart,
            dateEnd: hotelData.dateEnd,
            active: hotelData.active,
            region: hotelData.region,
        })
        if (this.data.region[0] != null) {
            this.getArea()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.getData()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // 处理上拉触底事件
        this.setData({
            loading: true
        });
        // 模拟加载数据
        setTimeout(() => {
            // 加载完成后隐藏加载提示
            this.setData({
                loading: false
            });
        }, 2000); // 这里的 2000 毫秒可以根据实际加载时间进行调整
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})